﻿package edzis.translatebuddy.view.components {
	import edzis.translatebuddy.events.DataProxyEvent;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.data.DataProvider;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	/**
	 * The View Component for adding a word to the translation list
	 * @author Edgars Simsons
	 */
	public class WatchWords extends WatchWords_design {
		private var _enterDown	:Boolean;
		
		public function WatchWords() {
			visible = false;
		}
		
		public function init(allWords:DataProvider) :void {
			wordList.columns = ["id", "word", "translation", "createdReadable", "updatedReadable"];
			wordList.columns.forEach(formatLabels);
		//	wordList.editable = true;
			wordList.dataProvider = allWords;
			wordTxt.dataProvider = allWords;
			wordTxt.addEventListener(Event.CHANGE, onWordChanged);
			wordTxt.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			wordTxt.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			findBtn.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			findBtn.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			findBtn.addEventListener(MouseEvent.CLICK, onLookupWord);
		}
		
		private function formatLabels(column:DataGridColumn, index:int, array:Array):void {
			column.headerText = column.dataField.substr(0,1).toUpperCase() + column.dataField.substr(1);
		}
		
		private function onWordChanged(e:Event):void {
			//
			trace("wordTxt.selectedItem " + wordTxt.selectedItem);
		//	if (wordTxt.selectedItem != "")
			
		}
		
		private function onLookupWord(e:MouseEvent):void {
		//	if (wordTxt.selectedLabel != "") dispatchEvent( new DataProxyEvent(DataProxyEvent.TRY_CREATE_SESSION, newSession_txt.text));
		}
		
		private function onKeyDown(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.ENTER && !_enterDown) {
				_enterDown = true;
			//	onLookupWord(null);
			}
		}
		
		private function onKeyUp(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.ENTER) _enterDown = false;
		}
		
		private function onWordUpdate(e:Event):void{
		}
		
		private function onTranslationUpdate(e:Event):void{
			
		}
		
	}
	
}